﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BootyBay.Models;
using BootyBay.Repository;

namespace BootyBay.ServerWebApp.Controllers
{
    [OutputCache(NoStore = true, VaryByParam = "*", Duration = 0)]
    public class StoreUserController : Controller
    {
        //
        // GET: /User/
        public ActionResult Index()
        {
            var repository = new RepositoryBase<StoreUser>();
            var users = repository.GetAll().ToList();
            return View(users);
        }

        [HttpPost]
        public ActionResult AddUser(StoreUser newUser)
        {
            var repository = new RepositoryBase<StoreUser>();

            newUser.IsValid = true;
            newUser.TimeStamp = DateTime.UtcNow;

            repository.Insert(newUser);

            var users = repository.GetAll().ToList();

            ViewBag.SuccessMessage = "User added.";
            return PartialView("_StoreUserList", users);
        }

        [HttpPost]
        public ActionResult RemoveUser(int id)
        {
            var repository = new RepositoryBase<StoreUser>();

            //set user as invalid
            var user = repository.GetByID(id);
            user.IsValid = false;
            repository.Update(user);

            var users = repository.GetAll().ToList();

            ViewBag.SuccessMessage = "User deleted.";
            return PartialView("_StoreUserList", users);
        }

        [HttpGet]
        public ActionResult ListApplication(string username)
        {
            var operations = AppOperationRecordRepository.Instance().GetAppOptRecord4User(username);

            ViewBag.Operations = operations;
            ViewBag.UserName = username;

            return PartialView("_ApplicationList", operations);
        }

        [HttpPost]
        public ActionResult SetOperation(string username, View_AppOptRecord4User record)
        {
            var repository = new RepositoryBase<AppOperationRecord>();


            //if (record.OperationType != AppOperationType.NA)
            //{ }
            AppOperationRecord a = new AppOperationRecord()
            {
                Id = 0,
                Id_ApplicationInfo = record.Id_Application,
                OperationType = (int)record.OperationType,
                TimeStamp = DateTime.UtcNow,
                UserName = username
            };

            repository.Insert(a);

            ViewBag.SuccessMessage = "Operation set.";




            var operations = AppOperationRecordRepository.Instance().GetAppOptRecord4User(username);

            ViewBag.Operations = operations;
            ViewBag.UserName = username;

            return PartialView("_ApplicationList", operations);
        }
    }
}